//
//  BYSettingViewController.m
//  School+
//
//  Created by Sean Sebastian on 4/28/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYSettingViewController.h"
#import "AppDelegate.h"

#import "UserAccount.h"
#import "SVProgressHUD.h"
#import "BYFooterView.h"
#import "BYLoginControllerViewController.h"

#import "BYFeedBackViewController.h"
#import "BYAboutViewController.h"
#import "BYProtocolViewController.h"


@interface BYSettingViewController () <FooterViewDelegate>

@end

@implementation BYSettingViewController

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad
{
    
    self.tabBarController.tabBar.hidden = YES;
    
    CGFloat color = 241.0/255.0;
    self.tableView.backgroundColor = [UIColor colorWithRed:color green:color blue:color alpha:1.0];

    
    BYFooterView *view = [[BYFooterView alloc] initWithFrame:CGRectMake(0, 0, 10, 100)];
    view.delegate = self;
    self.tableView.tableFooterView = view;

}

- (void)logoutButtonClick
{
    printf(__FUNCTION__);
    [UIView animateWithDuration:1 animations:^{
        [SVProgressHUD showWithStatus:@"登出中.."];
        sleep(1);
    } completion:^(BOOL finished) {
        sleep(1);
        // 推出用户登录
        NSLog(@"退出用户登录");
        [[UserAccount sharedUserAccount] deleteUserInfo];
        [UserAccount sharedUserAccount].isSignIn = NO;
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [(AppDelegate *)[UIApplication sharedApplication].delegate defaultController];
    }];
    
}

- (void)adminLoginButtonClick
{
    printf(__FUNCTION__);
    [UIView animateWithDuration:1 animations:^{
        [SVProgressHUD showWithStatus:@"登出中.."];
        sleep(1);
    } completion:^(BOOL finished) {
        sleep(1);
        // 推出用户登录
        NSLog(@"退出用户登录");
        [[UserAccount sharedUserAccount] deleteUserInfo];
        [UserAccount sharedUserAccount].isSignIn = NO;
        
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:[[BYLoginControllerViewController alloc] init]];
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = nvc;
    
    }];
    
    
    
    
    
}

// 无论点击的是推出退出账户还是登录后台管理，都需要退出当前用户账户

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    if (indexPath.section==0&&indexPath.row==0) {
        cell.textLabel.text = @"赏个好评";
    
    }else if(indexPath.section==0&&indexPath.row==1){
        cell.textLabel.text = @"意见反馈";
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }else if(indexPath.section==0&&indexPath.row==2){
        cell.textLabel.text = @"推荐给好友";

    }else if(indexPath.section==1&&indexPath.row==0 ){
        cell.textLabel.text = @"清除缓存";
    }else if (indexPath.section==1&&indexPath.row==1){
        cell.textLabel.text = @"关于";
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.textLabel.text = @"用户协议";
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld-%ld" , indexPath.section , indexPath.row);
    
    
    // insert cell click everts
    if (indexPath.section==0&&indexPath.row==0) {
        
        
        [SVProgressHUD showErrorWithStatus:@"没有上架，无法评分！"];
        
    }else if(indexPath.section==0&&indexPath.row==1){
        
        [self.navigationController pushViewController:[[BYFeedBackViewController alloc] init] animated:YES];
        
    }else if(indexPath.section==0&&indexPath.row==2){
        
        [SVProgressHUD showInfoWithStatus:@"友盟分享"];
        
        
    }else if(indexPath.section==1&&indexPath.row==0 ){
        
        [SVProgressHUD showInfoWithStatus:@"清除缓存"];
        

    }else if (indexPath.section==1&&indexPath.row==1){
        [self.navigationController pushViewController:[[BYAboutViewController alloc] init] animated:YES];

    }else{
        [self.navigationController pushViewController:[[BYProtocolViewController alloc] init] animated:YES];
    }
}

@end
